import { SettingGameRunBase } from "./SettingGameRun.generated";

const { regClass, property } = Laya;

@regClass()
export class SettingGameRun extends SettingGameRunBase {
    /**背景音乐是否静音 */
    private isMusicMuted: boolean = false;
    /**音效是否静音 */
    private isSoundMuted: boolean = false;
    /** 关闭时触发 */
    public actClose: () => void;
    onAwake(): void {
        this.init();
        this.addEventListener();
    }
    onDestroy(): void {
        this.removeEventListener();
    }

    public showView() {
        this.visible = true;
    }


    private addEventListener(): void {
        this.btnCloseSetting.on(Laya.Event.CLICK, this, this.onClickCloseSettingBtn);
        this.btnClosePrivacyPolicy.on(Laya.Event.CLICK, this, this.onClickClosePrivacyPolicy);
        this.btnCloseUserAgreement.on(Laya.Event.CLICK, this, this.onClickCloseUserAgreement);
        this.labelPrivacyPolicy.on(Laya.Event.CLICK, this, this.onClickOpenPrivacyPolicy);
        this.labelUserAgreement.on(Laya.Event.CLICK, this, this.onClickOpenUserAgreement);
        this.toggleMusicSwitch.on(Laya.Event.CLICK, this, this.onClickToggleMusicSwitch);
        this.toggleSoundSwitch.on(Laya.Event.CLICK, this, this.onClickToggleSoundSwitch);
    }

    private removeEventListener(): void {
        this.btnCloseSetting.off(Laya.Event.CLICK, this, this.onClickCloseSettingBtn);
        this.btnClosePrivacyPolicy.off(Laya.Event.CLICK, this, this.onClickClosePrivacyPolicy);
        this.btnCloseUserAgreement.off(Laya.Event.CLICK, this, this.onClickCloseUserAgreement);
        this.toggleMusicSwitch.off(Laya.Event.CLICK, this, this.onClickToggleMusicSwitch);
        this.toggleSoundSwitch.off(Laya.Event.CLICK, this, this.onClickToggleSoundSwitch);
    }

    private init() {
        this.isMusicMuted = false;
        this.isSoundMuted = false;
        this.imgUserAgreement.visible = false;
        this.imgPrivacyPolicy.visible = false;
        this.visible = false;
        this.userAgreementPanel.vScrollBar.autoHide = true;
        this.privacyPolicyPanel.vScrollBar.autoHide = true;
    }

    /**监听点击关闭 */
    private onClickCloseSettingBtn(): void {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        this.visible = false;
        this.actClose?.();
        this.actClose = undefined;// 触发后清空事件防止意外发生
    }
    /**监听点击背景音效开关 */
    private onClickToggleMusicSwitch(): void {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        this.isMusicMuted = !this.isMusicMuted;
        let skin: string = this.isMusicMuted ? "atlas/UI/Setting/switch_off@1x.png" : "atlas/UI/Setting/switch_on@1x.png";
        this.toggleMusicSwitch.skin = skin;
        let content: string = this.isMusicMuted ? "关闭" : "开启";
        console.log("背景音乐=>", content);
        Laya.SoundManager.musicMuted = this.isMusicMuted;
    }
    /**监听点击背景音乐开关 */
    private onClickToggleSoundSwitch(): void {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        this.isSoundMuted = !this.isSoundMuted;
        let skin: string = this.isSoundMuted ? "atlas/UI/Setting/switch_off@1x.png" : "atlas/UI/Setting/switch_on@1x.png";
        this.toggleSoundSwitch.skin = skin;
        let content: string = this.isSoundMuted ? "关闭" : "开启";
        console.log("音效=>", content);
        Laya.SoundManager.soundMuted = this.isSoundMuted;
    }
    /**监听点击关闭隐私政策页面 */
    private onClickOpenPrivacyPolicy(): void {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        this.imgPrivacyPolicy.visible = true;
        this.agreenmentAndPolicy.visible = false;
    }

    /**监听点击关闭用户协议页面 */
    private onClickOpenUserAgreement(): void {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        this.imgUserAgreement.visible = true;
        this.agreenmentAndPolicy.visible = false;
    }

    /**监听点击关闭隐私政策页面 */
    private onClickClosePrivacyPolicy(): void {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        this.imgPrivacyPolicy.visible = false;
        this.agreenmentAndPolicy.visible = true;
    }

    /**监听点击关闭用户协议页面 */
    private onClickCloseUserAgreement(): void {
        Laya.SoundManager.playSound("resources/Audio/audioClick.mp3", 1);
        this.imgUserAgreement.visible = false;
        this.agreenmentAndPolicy.visible = true;
    }
}